<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Folder {
    //put your code here
    public $id;
    public $userId;
    public $name;
    public $parentId;
    public $type;
    public $dateCreated;
    public $privacy;
    public $deleted;
    public static $instance;

    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    public function __get($name) {
        return $this->name;
    }
    
    public function __set($name, $value) {
        $this->name = $value;
    }
    
    public function __construct() {
        //allocate your stuff
    }
    
    /**
     * Read folder infos from the database
     * @param Int $id
     */
    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM folders WHERE id = '$id'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->name = $rows["name"];
                $instance->userId = $rows["userid"];
                $instance->parentId = $rows["parentid"];
                $instance->type = $rows["type"];
                $instance->dateCreated = $rows["datecreated"];
                $instance->privacy = $rows["privacy"];
                $instance->deleted = $rows["deleted"];
                return $instance;
            } else {
                $controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any folder with provided id");
                $controller->returnError();
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    /**
     * Create new Folder object to store
     * @param Int $userid
     * @param String $folderName
     * @param Int $parentId
     */
    public static function withData($userid, $folderName, $parentId, $folderType) {
        $instance = new self();
        $instance->userId = $userid;
        $instance->name = $folderName;
        $instance->parentId = $parentId;
        $instance->type = $folderType;
        return $instance;
    }
    
    public static function getId($name) {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM folders WHERE name = '$name'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                return $rows["id"];
            } else {
                $controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any folder with provided id");
                $controller->returnError();
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    public static function listFolder($userId) {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM folders WHERE userid = '0' OR userid = '$userId' AND deleted == '0'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $folders = array();
                foreach ($result->dataTable->rows as $row) {
                    $folder = new self();
                    $folder->id = $row["id"];
                    $folder->name = $row["name"];
                    $folder->userId = $row["userid"];
                    $folder->parentId = $row["parentid"];
                    $folder->type = $row["type"];
                    $folders[] = $folder;
                }
                return $folders;
            } else {
                $controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any folder with provided id");
                $controller->returnError();
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }
}

